package cn.rongdao.jrkabao.ui.main.mainfragment.vip;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.ui.activity.FRStartActivity;
import com.mifengkong.frtools.util.FRCollection;
import com.mifengkong.frtools.util.FRImageLoad;
import com.mifengkong.frtools.util.FRString;
import com.mifengkong.frwidget.titlebar.TitleBarUIComponent;

import butterknife.BindView;
import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.base.BaseAutoFinishActivity;
import cn.rongdao.jrkabao.bean.deposit.AccountInfoBean;
import cn.rongdao.jrkabao.bean.main.vip.MemberBean;
import cn.rongdao.jrkabao.model.main.vip.MemberModel;
import cn.rongdao.jrkabao.ui.main.mainfragment.uicomponent.MemberPowerUIComponent;
import cn.rongdao.jrkabao.util.CommonUtil;

/**
 * Created by jack on 2018/1/26
 */

public class MyMemberActivity extends BaseAutoFinishActivity {

    public static final String ACCOUNT_BEAN = "account_info_bean";

    public static final int MEMBER_REQUEST_CODE = 0x321;

    public static void start(Context context, AccountInfoBean accountInfoBean) {
        Bundle bundle = new Bundle();
        bundle.putParcelable(ACCOUNT_BEAN, accountInfoBean);
        FRStartActivity.start(context, MyMemberActivity.class, bundle);
    }

    @BindView(R.id.ammem_titlebar)
    TitleBarUIComponent titleBarUIComponent;
    @BindView(R.id.ammem_rl_info)
    RelativeLayout rlInfo;
    @BindView(R.id.ammem_rl_avatar)
    RelativeLayout rlAvatar;
    @BindView(R.id.ammem_iv_avatar)
    ImageView ivAvatar;
    @BindView(R.id.ammem_iv_vip)
    ImageView ivVip;
    @BindView(R.id.ammem_tv_phone)
    TextView tvPhone;
    @BindView(R.id.ammem_tv_member)
    TextView tvMember;
    @BindView(R.id.ammem_tv_renew)
    TextView tvReNew;
    @BindView(R.id.ammem_member_power)
    MemberPowerUIComponent memberPowerUIComponent;

    private AccountInfoBean accountInfoBean;

    @Override
    protected int layoutRes() {
        return R.layout.activity_my_member;
    }

    @Override
    protected void initView() {
        super.initView();
        initTitleBar();

        initData();
        initListener();
    }

    private void initListener() {
        tvReNew.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MemberActivity.startForResult(thisActivity(), true);
            }
        });
    }

    private void initData() {
        accountInfoBean = getIntent().getParcelableExtra(ACCOUNT_BEAN);
        setAccount();
        MemberModel.getInstance().execute(new FRBaseObserver<MemberBean>(this, "加载中，请稍候...") {
            @Override
            protected void onBaseNext(MemberBean data) {
                if (null != data.getVip_privilege()) {
                    memberPowerUIComponent.setContent(data.getVip_privilege());
                }
                memberPowerUIComponent.setLinkUrl(data.getVip_service_agreement());
            }
        });
    }

    private void setAccount() {
        if (null != accountInfoBean) {
            rlInfo.setVisibility(View.VISIBLE);
            rlInfo.setEnabled(CommonUtil.isStatusSuccess(accountInfoBean.getMember_status()));
            FRImageLoad.loadCircle(accountInfoBean.getUser_logo(), ivAvatar, R.drawable.icon_default_avatar);
            tvPhone.setText(FRString.valueOf(accountInfoBean.getPhone()));
            setMember();
        }
    }

    private void setMember() {
        String content = "";
        if (CommonUtil.isStatusSuccess(accountInfoBean.getMember_status())) {
            content = FRCollection.appendAsString("VIP会员\n", accountInfoBean.getMember_day(), "到期", "");
            ivVip.setVisibility(View.VISIBLE);
            rlAvatar.setBackgroundResource(R.drawable.member_ring_bg);
        } else {
            ivVip.setVisibility(View.GONE);
            rlAvatar.setBackground(null);
        }
        if (accountInfoBean.getMember_status() == 2) {
            content = FRCollection.appendAsString("会员权益\n已离开您", accountInfoBean.getOverdue_day(), "天啦", "");
        }
        tvMember.setText(content);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == MEMBER_REQUEST_CODE) {
            accountInfoBean = data.getParcelableExtra(ACCOUNT_BEAN);
            if (null != accountInfoBean) {
                setAccount();
            }
        }
    }

    private void initTitleBar() {
        titleBarUIComponent.initTitle("我的VIP会员");
        titleBarUIComponent.setBackgroundColor(ContextCompat.getColor(this, R.color.colorPrimary));
        titleBarUIComponent.initLeftBack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        titleBarUIComponent.initRight("购买记录", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                VipPurchaseRecordActivity.start(thisActivity());
            }
        });
    }
}